package com.spider.silence.worker;

import com.spider.silence.abstarct.downloader.AbstractCommonDownLoader;
import com.spider.silence.task.Njd1ModuleTask;
import com.spider.silence.task.Njd1Task;
import com.spider.silence.task.manager.CommonTaskManager;
import com.spider.silence.utils.PropertiesUtil;
import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Administrator
 * @description 描述
 * @time 2017/4/17 0017
 */
public class Njd1ModuleWorker  extends AbstractCommonDownLoader implements Runnable{
    private Logger logger = Logger.getLogger(Njd1ModuleWorker.class);


    @Override
    public void run() {

        int total_page = 1;
        while(true&&!Thread.interrupted()) {
            try {
                Njd1ModuleTask moduleTask = (Njd1ModuleTask) CommonTaskManager.takeTask("Njd1Module",1000);
                if(null!=moduleTask) {

                    String module_url = moduleTask.getModule_url();


                    long future = System.currentTimeMillis() + Long.valueOf(PropertiesUtil.getVal("KaiDi.GetForumUrlTime"));
                    long remianing = Long.valueOf(PropertiesUtil.getVal("KaiDi.GetForumUrlTime"));
                    boolean flag = true;
                    List<Elements> elementsList = new ArrayList<Elements>();
                    while (flag && remianing >= 0) {
                        try {
                            remianing = future - System.currentTimeMillis();
                            Document document = get(module_url);
                            Elements urlElements = document.select("#nv_forum").select(".boardnavl").select(".comiis_width").select("#ct").select(".mn");

                            String total = urlElements.select("#pgt").select(".pg").select("label").get(0).text();


                            String regex = "[0-9]{1,}";
                            Pattern p = Pattern.compile(regex);
                            Matcher m = p.matcher(total);
                            while (m.find()) {
                                total_page = Integer.parseInt(m.group());
                            }

                            Elements forumUrlList = urlElements.select("#threadlist").select(".bm_c").select("#moderate").select("#threadlisttableid").select("tbody[id^='normalthread_']");

                            for (int j = 0; j < forumUrlList.size(); j++) {
                                String content_url = forumUrlList.get(j).select(".icn").select("a").get(0).attr("href");
                                Njd1Task task = new Njd1Task(URLDecoder.decode(content_url, "UTF-8"));
                                CommonTaskManager.addTask("Njd1", task, 1000);
                                System.out.println(content_url);
                            }

                            flag = false;

                        } catch (Exception e) {
                            logger.error(e.getMessage(), e);
                            e.printStackTrace();
                        }
                    }

//                    if (total_page >= 2) {
//                        for (int j = 0; j < total_page; j++) {
//                            String new_module_url = module_url.split("-")[0] + "-" + module_url.split("-")[1] + "-" + j + ".html";
//                            Document document = get(new_module_url);
//                            Elements urlElements = document.select("#nv_forum").select(".boardnavl").select(".comiis_width").select("#ct").select(".mn");
//                            Elements forumUrlList = urlElements.select("#threadlist").select(".bm_c").select("#moderate").select("#threadlisttableid").select("tbody[id^='normalthread_']");
//
//                            for (int n = 0; n < forumUrlList.size(); n++) {
//                                String content_url = forumUrlList.get(n).select(".icn").select("a").get(0).attr("href");
//                                Njd1Task task = new Njd1Task(URLDecoder.decode(content_url, "UTF-8"));
//                                CommonTaskManager.addTask("Njd1", task, 1000);
//                                System.out.println(content_url);
//                            }
//
//                        }
//                    }
                }
            }catch (Exception e){
                logger.error(e.getMessage(),e);
                continue;
            }
        }
    }
}
